﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.Logger.Entity;

namespace Wicresoft.Logger
{
    public static class Log
    {
        private static LogContext logDb = new LogContext();

        /// <summary>
        /// 提供集中的日志记录方式，日志将被写入预设的数据库表
        /// </summary>
        /// <param name="task">发生日志的任务</param>
        /// <param name="message">日志内容</param>
        /// <param name="e">异常信息，可以为空</param>
        public static void Info(string task, string message, params object[] objs)
        {
            var entity = new Entity.Log
            {
                CreatedTime = DateTime.Now,
                Verb = "Info",
                TaskName = task,
                Message = string.Format(message, objs),
            };

            Push(entity);
        }

        public static void Debug(string task, string message, params object[] objs)
        {
            var entity = new Entity.Log
            {
                CreatedTime = DateTime.Now,
                Verb = "Debug",
                TaskName = task,
                Message = string.Format(message, objs),
            };

            Push(entity);
        }

        public static void Error(string task, string message, params object[] objs)
        {
            var entity = new Entity.Log
            {
                CreatedTime = DateTime.Now,
                Verb = "Error",
                TaskName = task,
                Message = string.Format(message, objs),
            };

            Push(entity);
        }

        public static void Exception(string task, Exception e)
        {
            var entity = new Entity.Log
            {
                CreatedTime = DateTime.Now,
                Verb = "Exception",
                TaskName = task,
                Message = e.Message,
                Exception = e.ToString(),
            };

            Push(entity);
        }

        private static void Push(Entity.Log entity)
        {
            lock (logDb)
            {
                logDb.Logs.Add(entity);
                logDb.SaveChanges();
            }
        }
    }
}
